//
//  HOPWelcomeControllerViewController.swift
//  WB01
//
//  Created by HOP on 15/12/17.
//  Copyright © 2015年 HOP. All rights reserved.
//

import UIKit

import SDWebImage

class HOPWelcomeController: UIViewController {
    // MARK: - 属性
    /// 头像底部约束, 在初始化的时候确定不了约束,在 prepareUI 里面才能确定
    private var iconViewBottonCon:NSLayoutConstraint?
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
        // 2. 先显示上一次保存的头像
        setIconView()
        // 3. 加载最新的用户头像
        HOPuserAccount.loadUserAccount()?.loadUserInfo({ (error) -> () in
            // 判断是否有错误
            if error != nil {
            print("获取用户信息出错了")
            return
            
            }
            // 获取用户数据没有出错,加载最新的头像
            self.setIconView()
        })
        
    }
    /// 显示当前用户头像
    private func setIconView() {
    // 获取当前的用户头像地址
        if let avatar_large = HOPuserAccount.loadUserAccount()?.avatar_large {
        let url = NSURL(string: avatar_large)!
            print("头像 url: \(url)")
        
        // 显示当前用户头像
        
        self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        
        
        
        }
    
    
    
    }
    // view看的到的时候做动画
    override func viewDidAppear(animated: Bool) {
        // 头像做动画,修改头像底部约束的值,往上是负数
        iconViewBottonCon?.constant = -(UIScreen.mainScreen().bounds.height - 160)
        // 带弹簧效果
        // usingSpringWithDamping: 弹簧的效果, 范围是0 - 1, 越小越明显
        // initialSpringVelocity: 初始速度
UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
    // 动画
    // 闭包里面需要加self
    self.view.layoutIfNeeded()
    }) { (_) -> Void in
        // 将label渐变出来
        self.textLabel.alpha = 0
        UIView.animateWithDuration(1, animations: { () -> Void in
            self.textLabel.alpha = 1
            self.textLabel.hidden = false
            },completion: {(_) -> Void in
        // 切换界面
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootViewController(true)
        
        })
        
        }
    }
    //MARK - 准备UI
    //准备UI
    private func prepareUI() {
    //添加子控件
    view.addSubview(bkgImageView)
    view.addSubview(iconView)
    view.addSubview(textLabel)
    //取消Autoresizing
    bkgImageView.translatesAutoresizingMaskIntoConstraints = false
    iconView.translatesAutoresizingMaskIntoConstraints = false
    textLabel.translatesAutoresizingMaskIntoConstraints = false
        //添加约束
        //背景
//        宽
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
//        顶部
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        底部
        view.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
//            头像 
//        CenterX与夫控件的CenterX重合
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconViewBottonCon = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160)
        view.addConstraint(iconViewBottonCon!)
        // 宽度
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        // 高度
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))

        //文字
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        view.addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: .CenterX, multiplier: 1, constant: 0))

    }
     //MARK - 懒加载
    //背景图片
    private lazy var bkgImageView: UIImageView = {
    let imageView = UIImageView(image: UIImage(named: "ad_background"))
       return imageView
    }()
    //头像
    private lazy var iconView: UIImageView = {
    let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
     return iconView
    }()
    //用户名称
    private lazy var textLabel: UILabel = {
    let textLabel = UILabel()
    textLabel.text = "欢迎归来"
        textLabel.hidden = true
    textLabel.textColor = UIColor.darkGrayColor()
    textLabel.sizeToFit()
    return textLabel
    }()
    
}
